File: //opt/alt/python37/lib/python3.7/site-packages/lvestats/plugins/other/lvedestroyer.py
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from __future__ import absolute_import
from builtins import map
from lvestats.core.plugin import LveStatsPlugin
import subprocess
import logging
# LVE_VERSION = parseLVEVersion()
LVECTL = '/usr/sbin/lvectl'
class LVERecord(object):
def __init__(self, cpu, iterations):
self.cpu = cpu
self.count = 0
self.iterations = iterations
def inc(self):
self.count += 1
def check(self):
return self.count >= self.iterations
def has_changed(self, stat):
if self.cpu != stat.cpu or stat.nproc > 0:
self.count = 0
self.cpu = stat.cpu
return True
return False
class LVEDestroyer(LveStatsPlugin):
def __init__(self):
self.log = logging.getLogger(__name__)
def set_config(self, config):
self._enabled = False
try:
self.iterations = config['iterations']
if self.iterations != 0:
self._enabled = True
self.lves = {}
self.default_stat = None
self.iterations = 0
except KeyError:
pass
def set_default(self, stat):
if self._enabled:
self.default_stat = stat
self.to_be_destroyed = []
def add_stat(self, stat):
if self._enabled:
id = stat.id
if id == 0:
self.set_default(stat)
return
if id in self.lves:
r = self.lves[id]
if not r.has_changed(stat):
if r.check():
self.to_be_destroyed.append(id)
self.lves.pop(id)
else:
r.inc()
# if any of this LVE's settings differ from the default, then don't track this LVE for destruction
elif ((stat.cpu != self.default_stat.cpu)
or (stat.io != self.default_stat.io)
or (stat.lmem != self.default_stat.lmem)
or (stat.lep != self.default_stat.lep)
or (stat.lmemphy != self.default_stat.lmemphy)
or (stat.lcpuw != self.default_stat.lcpuw)
or (stat.lnproc != self.default_stat.lnproc)):
return
else:
self.lves[id] = LVERecord(stat.cpu, self.iterations)
def process_destroy(self):
if self._enabled:
if self.to_be_destroyed:
try:
# run the command and suppress it's output
p = subprocess.Popen(
[LVECTL, "destroy-many"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# send input string to suprocess
p.communicate(" ".join(map(str, self.to_be_destroyed)))
# print "Destroying: ", " ".join(map(str, self.to_be_destroyed))
except OSError:
self.log.error("Error: failed to run %s destroy-many", LVECTL)
def execute(self, lve_data):
stats = lve_data['stats']
self.set_default(stats[0])
for stat in stats:
self.add_stat(stat)
self.process_destroy()